package com.android.boncen.commonFrame.base;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.AsyncTaskLoader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.boncen.commonFrame.R;
import com.android.boncen.commonFrame.activity.Webview_detail_activity;
import com.android.boncen.commonFrame.bean.KnowledgeBean;
import com.android.boncen.commonFrame.bean.Knowledge_lists_Bean;
import com.android.boncen.commonFrame.tools.DesityUtil;
import com.google.gson.Gson;
import com.show.api.ShowApiRequest;

import org.xutils.image.ImageOptions;
import org.xutils.x;
import java.util.List;
/**
 * Created by Boncen on 2017/1/21.
 */

public class Knowledge_baseFrament extends Fragment {
    private int id; //用于请求知识详情
    private String title;
    Context mContext;
    View rootView;
    ListView knowledge_lists;

    Knowledge_lists_Bean knowledge_lists_bean;

    List<Knowledge_lists_Bean.ShowapiResBodyBean.PagebeanBean.ContentlistBean> contentlist;
    Knowledge_lists_Bean.ShowapiResBodyBean.PagebeanBean.ContentlistBean contentlistBean;
    private String json_list;
    private ImageOptions imageOptions;
    public  Knowledge_baseFrament(){}

    public  Knowledge_baseFrament(Context context,String title, int id){
        this.mContext = context;
        this.title = title;
        this.id = id;
        imageOptions = new ImageOptions.Builder().setRadius(DesityUtil.dip2px(mContext,5)).setCrop(true).setImageScaleType(ImageView.ScaleType.CENTER_CROP).build();

    }


    public String getTitle() {
        return title;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //上下文
        mContext = getActivity();
        new MyAsyncTask().execute();
        /**
         * 联网操作
         */
//        getKnowledgeListJson(id);
//        SystemClock.sleep(1000);
//        knowledge_lists_bean= handleListsJson(json_list);
//        contentlist = knowledge_lists_bean.getShowapi_res_body().getPagebean().getContentlist();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(rootView == null){
            rootView = View.inflate(getContext(), R.layout.knowledge_viewpager,null);
        }
        knowledge_lists = (ListView) rootView.findViewById(R.id.knowledge_lists);
        initListView();
        knowledge_lists.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(contentlist==null){return;}
                Intent intent = new Intent(mContext, Webview_detail_activity.class);
                intent.putExtra("title",contentlist.get(position).getTitle());
                intent.putExtra("url",contentlist.get(position).getWapurl());
                mContext.startActivity(intent);
            }
        });
        return rootView;
    }

    private void initListView() {
        knowledge_lists.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                if (contentlist!=null) {
                    return contentlist.size();
                }
                return 10;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder = null;
                if(convertView==null){
                    convertView=View.inflate(mContext,R.layout.knowledge_lists,null);
                    holder = new ViewHolder();
                   holder.list_pic = (ImageView) convertView.findViewById(R.id.knowledge_list_pic);
                    holder.title = (TextView) convertView.findViewById(R.id.knowledge_list_title);
                    holder.ctitle = (TextView) convertView.findViewById(R.id.knowledge_list_ctitle);
                    holder.list_time = (TextView) convertView.findViewById(R.id.knowledge_list_time);
                    convertView.setTag(holder);
                }else{
                    holder = (ViewHolder) convertView.getTag();
                }
               if(contentlist!=null){
                   contentlistBean = contentlist.get(position);
                   holder.title.setText(contentlistBean.getTitle());
                   holder.ctitle.setText(contentlistBean.getMedia_name());
                   holder.list_time.setText(contentlistBean.getCtime().substring(0,10));
                   String imageUrl = contentlistBean.getImg();
                   if(imageUrl==null||imageUrl==""){
                       holder.list_pic.setImageResource(R.drawable.nopic);
                   }else{
                       x.image().bind(holder.list_pic,imageUrl,imageOptions);
                   }

               }

                return convertView;
            }
        });
    }
    public static  class ViewHolder{
        ImageView list_pic;
        TextView title ;
        TextView ctitle ;
        TextView list_time ;
    }


    public Knowledge_lists_Bean handleListsJson(String json){
        return new Gson().fromJson(json,Knowledge_lists_Bean.class);
    }
    private  void getKnowledgeListJson(final int tid){
        new Thread(){
            @Override
            public void run() {
                String appid = "31257";
                String secret = "0ad89f99ef1041f1bcfb14689bfe84ba";
                 json_list = new ShowApiRequest("http://route.showapi.com/90-87", appid, secret).addTextPara("tid", tid + "").post();
            }
        }.start();
    }


    class MyAsyncTask extends AsyncTask<Integer,Integer,Boolean>{

        @Override
        protected Boolean doInBackground(Integer... params) {
          getKnowledgeListJson(id);
            SystemClock.sleep(2000);
            return true;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
//            Toast.makeText(mContext, "done", Toast.LENGTH_SHORT).show();
//            Toast.makeText(mContext, json_list, Toast.LENGTH_SHORT).show();
            knowledge_lists_bean= handleListsJson(json_list);
            contentlist = knowledge_lists_bean.getShowapi_res_body().getPagebean().getContentlist();
            knowledge_lists.invalidate();
        }
    }
}
